/* replication.do --- 
 * 
 * Filename: replication.do
 * Description: 
 * Author: Kai Arzheimer
 * Maintainer: 
 * Created: Mon Apr  2 14:58:18 2012 (+0200)
 * Version: 
 * Last-Updated: Mon Apr  2 15:00:45 2012 (+0200)
 *           By: Kai Arzheimer
 *     Update #: 3
 * URL: 
 * Keywords: 
 * Compatibility: 
 * 
 */

/* Commentary: 
 * Replication code for Arzheimer/Evans 2012 (Political Geography)
 * 
 * 
 */

/* Change log:
 * 
 * 
 */

/* Code: */



use replication, replace

* Wrap estimation of four models into dummy program so that I can easily test for exlucding very large wards

capture program drop runfourmodels

program define runfourmodels
* Distance + incumbency. 
	clogit votechoice conservatives liberal_democrats aq16_   drdistp incumbencyp if touse ,group(id) cluster(aconsnam)
	est store incumb

* Model w/o incumbency
	clogit votechoice conservatives liberal_democrats aq16_   drdistp  if touse ,group(id) cluster(aconsnam)
	est store pure

* Unpack incumbency
	clogit votechoice conservatives liberal_democrats aq16_   incparty inccand drdistp  if touse ,group(id) cluster(aconsnam)
	est store unpack

* Additional check: deprivation distance
	clogit votechoice conservatives liberal_democrats aq16_   inccand drdistp  absdepdist if touse ,group(id) cluster(aconsnam)
	est store deprivation
*	estwrite deprivation,id(pseudoid) replace
end program

* First run: use data as is
runfourmodels 


* Produce table
* coeflabels works with factor variables but needs the levels' true names (i.e. run w/o labels initially) 
* requires compound double quotes
local commonesttabcalloptions = `", label title(Conditional Logit)  nomtitles replace coeflabels (incumbencyp "Incumbency" incparty "Incumbent Party" inccand "Incumbent Candidate"  absdepdist "Deprivation Distance") se pr2 bic"'
local commonesttabcallmodels "pure incumb unpack deprivation"
esttab `commonesttabcallmodels' using "clogit-table.txt"  `commonesttabcalloptions'



************************************************************
* Additional tests
************************************************************

* Reviewers are worried about nonlinear effects of distance and length of incumbency. We check separately using fracpoly

* Distance
fracpoly: clogit votechoice  drdistp conservatives liberal_democrats aq16_   incparty inccand  absdepdist if touse ,group(id) cluster(aconsnam)
est store nonlindist
* More complex model does not fit signficantly fit better
est stats deprivation nonlindist

* More nuanced effect of incumbency
* Duration of parliamentary experience
replace first = 2010 if first ==.
gen int duration = 2010-first
fracpoly: clogit votechoice duration conservatives liberal_democrats aq16_    drdistp  absdepdist if touse ,group(id) cluster(aconsnam)
est store nonlindur
est stat deprivation nonlindist nonlindur
* Again, not really better. A fairer comparison would drop incparty from deprivation (it is insignificant
* and not in nonlindur), which would give an even clearer result

* Ok, so we stick with the original model


* Second run: drop wards 25sqm and larger 
preserve
replace touse = 0 if wardsqm >= 25
runfourmodels
esttab `commonesttabcallmodels' using "clogit-table-lt25sqm.txt"  `commonesttabcalloptions'
restore


* small program makes collecting statistics mildly more efficient
capture program drop matrixappend

program define matrixappend
	matrix vote = r(p)*100
	matrix prob = prob \ vote'
end program

asprvalue, cat(conservatives liberal_democrats) base(labour) rest(asmean)
matrix prob = r(asv)
matrixappend
* Everyone is local
asprvalue, cat(conservatives liberal_democrats) base(labour) x(drdistp 26 26 26) rest(asmean)
matrixappend

* Consvervative not local
asprvalue, cat(conservatives liberal_democrats) base(labour) x(drdistp 120 26 26) rest(asmean)
matrixappend
* LibDem not local
asprvalue, cat(conservatives liberal_democrats) base(labour) x(drdistp 26 120 26) rest(asmean)
matrixappend
* Labour not local
asprvalue, cat(conservatives liberal_democrats) base(labour) x(drdistp 26 26 120) rest(asmean)
matrixappend

*rename rows
matrix rownames prob = aq16_  inccand drdistp absdepdist real s1 s2 s3 s4

*output
local commonproboptions = `", label  replace coeflabel (incparty "Incumbent Party" inccand "Incumbent Candidate" real "Real" s1 "Scenario 1" s2 "Scenario 2" s3 "Scenario 3" s4 "Scenario 4")"'
esttab matrix(prob,fmt(a3)) using "probabilities-table.txt" `commonproboptions' 


/* replication.do ends here */
